﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using Microsoft.Office.Interop.Word;
namespace OA.aspx.DsoFramer
{
    public partial class ReadFile : System.Web.UI.Page
    {
        public string FileType = "";
        public string Src = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            this.HyperLink1.NavigateUrl = "/UpLoadFile/" + Request.QueryString["FilePath"].ToString();
            this.HyperLink1.Text = "[" + FTD.DBUnit.DbHelperSQL.GetSHSL("select OldName from ERPSaveFileName where NowName='" + Request.QueryString["FilePath"].ToString().Replace("MailAttachments/", "") + "'") + "]";
            try
            {
                FileType = Request.QueryString["FilePath"].ToString().Remove(0, Request.QueryString["FilePath"].ToString().LastIndexOf('.') + 1);
                //if (FileType.ToLower().Trim() == "doc" || FileType.ToLower().Trim() == "xls" || FileType.ToLower().Trim() == "ppt")
                //{

                //}
                //else
                //{
                //    Response.Write("<script>alert('该文件格式不能进行在线阅读！');window.close();</script>");
                //}
                var path = Server.MapPath(this.HyperLink1.NavigateUrl);

                var url = this.HyperLink1.NavigateUrl;
                string physicalPath = Server.MapPath(Server.UrlDecode(url));
                string extension = Path.GetExtension(physicalPath);

                string htmlUrl = "";
                switch (extension.ToLower())
                {
                    case ".xls":
                    case ".xlsx":
                        htmlUrl = PreviewExcel(physicalPath, url);
                        break;
                    case ".doc":
                    case ".docx":
                        htmlUrl = PreviewWord(physicalPath, url);
                        break;
                    case ".txt":
                        htmlUrl = PreviewTxt(physicalPath, url);
                        break;
                    case ".pdf":
                        htmlUrl = PreviewPdf(physicalPath, url);
                        break;
                    case ".jpg":
                    case ".jpeg":
                    case ".bmp":
                    case ".gif":
                    case ".png":
                        htmlUrl = PreviewImg(physicalPath, url);
                        break;
                    default:
                        htmlUrl = PreviewOther(physicalPath, url);
                        break;
                }

                 Response.Redirect( htmlUrl);
               // Src =  Request.Url.Authority+htmlUrl ;
            }
            catch (Exception ex)
            {
                var s= ex.ToString();
                Response.Write("<script>alert('该文件格式不能进行在线阅读！');window.close();</script>");
            }
        }


        #region 预览Excel
        /// <summary>
        /// 预览Excel
        /// </summary>
        public string PreviewExcel(string physicalPath, string url)
        {
            Microsoft.Office.Interop.Excel.Application application = null;
            Microsoft.Office.Interop.Excel.Workbook workbook = null;
            application = new Microsoft.Office.Interop.Excel.Application();
            object missing = Type.Missing;
            object trueObject = true;
            application.Visible = false;
            application.DisplayAlerts = false;
            workbook = application.Workbooks.Open(physicalPath, missing, trueObject, missing, missing, missing,
                missing, missing, missing, missing, missing, missing, missing, missing, missing);
            //Save Excel to Html
            object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
            string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";
            String outputFile = Path.GetDirectoryName(physicalPath) + "\\" + htmlName;
            workbook.SaveAs(outputFile, format, missing, missing, missing,
                              missing, XlSaveAsAccessMode.xlNoChange, missing,
                              missing, missing, missing, missing);
           
            workbook.Close();
            application.Quit();
            return Path.GetDirectoryName(Server.UrlDecode(url)) + "\\" + htmlName;
        }
        #endregion

        #region 预览Word
        /// <summary>
        /// 预览Word
        /// </summary>
        public string PreviewWord(string physicalPath, string url)
        {
            try
            {
                Microsoft.Office.Interop.Word._Application application = null;
                Microsoft.Office.Interop.Word._Document doc = null;
                application = new Microsoft.Office.Interop.Word.Application();
                object missing = Type.Missing;
                object trueObject = true;
                application.Visible = false;
                application.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                doc = application.Documents.Open(physicalPath, missing, trueObject, missing, missing, missing,
                    missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                //Save Excel to Html
                object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
                string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";
                String outputFile = Path.GetDirectoryName(physicalPath) + "\\" + htmlName;
                doc.SaveAs(outputFile, format, missing, missing, missing,
                                  missing, XlSaveAsAccessMode.xlNoChange, missing,
                                  missing, missing, missing, missing);
                doc.Close();
               
                application.Quit();
                return Path.GetDirectoryName(Server.UrlDecode(url)) + "\\" + htmlName;
            }
            catch (Exception e)
            {
                return "";
            }
        }
        #endregion

        #region 预览Txt
        /// <summary>
        /// 预览Txt
        /// </summary>
        public string PreviewTxt(string physicalPath, string url)
        {
            return Server.UrlDecode(url);
        }
        #endregion

        #region 预览Pdf
        /// <summary>
        /// 预览Pdf
        /// </summary>
        public string PreviewPdf(string physicalPath, string url)
        {
            return Server.UrlDecode(url);
        }
        #endregion

        #region 预览图片
        /// <summary>
        /// 预览图片
        /// </summary>
        public string PreviewImg(string physicalPath, string url)
        {
            return Server.UrlDecode(url);
        }
        #endregion

        #region 预览其他文件
        /// <summary>
        /// 预览其他文件
        /// </summary>
        public string PreviewOther(string physicalPath, string url)
        {
            return Server.UrlDecode(url);
        }
        #endregion



    }
}